package com.java.controller;

import com.java.pojo.User;
import com.java.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("login")
    public void login(Integer loginName, String passWord, HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = userService.login(loginName, passWord);
        PrintWriter writer = response.getWriter();
        response.setContentType("text/html;charset=utf-8");
        if (user != null) {  //登录成功
            if (user.getUserstate() != 0) { //查看用户状态
                writer.print("<script>alert('此用户被禁用，请联系管理员');location.href='http://localhost/login.html'</script>");
            } else { //登录成功，返回首页
                request.getSession().setAttribute("user", user);
                writer.print("<script>location.href='http://localhost/index.html'</script>");
            }
        } else {  //登录失败，判断用户名或密码错误
            User user1 = userService.selectByPrimaryKey(loginName);
            if (user1 != null) {
                writer.print("<script>alert('密码错误');location.href='http://localhost/login.html'</script>");
            } else {
                writer.print("<script>alert('账号错误');location.href='http://localhost/login.html'</script>");
            }
        }
    }

    @RequestMapping("getName")
    @ResponseBody
    public User getName(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return user;
    }

    @RequestMapping("loginOut")
    @ResponseBody
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("user");
        PrintWriter writer = response.getWriter();
        writer.print("<script>alert('退出成功');location.href='http://localhost/login.html'</script>");
    }

    @RequestMapping("password")
    public String password() {
        return "User/password";
    }

    @RequestMapping("updatePassword")
    public void updatePassword(String password, int loginName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        int updatePassword = userService.updatePassword(password, loginName);
        System.out.println(updatePassword);
        PrintWriter writer = response.getWriter();
        if (updatePassword > 0) {
            request.getSession().removeAttribute("user");
            writer.println("<script>alert('修改成功');top.location.href='http://localhost/login.html'</script>");
        }else {
            writer.print("<script>alert('修改失败');location.href='http://localhost/User/password.html'</script>");
        }
    }
}
